/****************************************Copyright (c)**************************************************
**                                         2016-03
**
**                                
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name:		    ht6x1x_uart.c
** Last modified Date:  2016-03-29
** Last Version:		v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Created by:			Evan Chen
** Created date:		2016-03-29
** Version:				v1.0
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __HT6X1X_UART_H
#define __HT6X1X_UART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "ht60xx.h"

   
/** 
  * @brief  USART Init Structure definition  
  */ 

typedef struct
{
  uint32_t UART_BaudRate;           
  uint32_t UART_WordLength;          
  uint32_t UART_StopBits;           
  uint32_t UART_Parity;              
  uint32_t UART_Mode;  
} UART_InitTypeDef;

typedef struct
{
  uint32_t IR_Lvl;           
  uint32_t IR_Enable;          
  uint32_t IR_Duty; 
} IR_InitTypeDef;

typedef struct
{
  uint32_t ISO7816_BaudRate;           
  uint32_t ISO7816_AutoRxTx;          
  uint32_t ISO7816_AutoRxTxRepeat; 
  uint32_t ISO7816_AckLen; 
  uint32_t ISO7816_Parity;              
  uint32_t ISO7816_Mode;  
} ISO7816_InitTypeDef;


/* Exported constants --------------------------------------------------------*/

/** @defgroup USART_Exported_Constants
  * @{
  */ 

#define IS_UART_ALL_PERIPH(PERIPH) (((PERIPH) == HT_UART0) || \
                                     ((PERIPH) == HT_UART1) || \
                                     ((PERIPH) == HT_UART2) || \
                                     ((PERIPH) == HT_UART3) || \
                                     ((PERIPH) == HT_UART4) || \
                                     ((PERIPH) == HT_UART5))


/*MODESEL*/
#define MODESEL_MODE_MASK	  ((uint16_t)0x0001)
#define MODESEL_MODE_UART     ((uint16_t)0x0000)
#define MODESEL_MODE_7816     ((uint16_t)0x0001)

/*UARTCON*/
#define UARTCON_UNEG_MASK	  ((uint16_t)0x0200)
#define UARTCON_UNEG_POSITIVE ((uint16_t)0x0000)
#define UARTCON_UNEG_NEGATIVE ((uint16_t)0x0200)


#define UARTCON_STOPSEL_MASK  ((uint16_t)0x0100)
#define UARTCON_STOPSEL_2BIT  ((uint16_t)0x0100)
#define UARTCON_STOPSEL_1BIT  ((uint16_t)0x0000)

#define UARTCON_LENSEL_MASK	  ((uint16_t)0x0080)
#define UARTCON_LENSEL_7BIT	  ((uint16_t)0x0080)
#define UARTCON_LENSEL_8BIT	  ((uint16_t)0x0000)

#define UARTCON_PARITYSEL_MASK  ((uint16_t)0x0070)
#define UARTCON_PARITYSEL_1     ((uint16_t)0x0070)
#define UARTCON_PARITYSEL_EVEN  ((uint16_t)0x0050)
#define UARTCON_PARITYSEL_ODD   ((uint16_t)0x0030)
#define UARTCON_PARITYSEL_0     ((uint16_t)0x0010)
#define UARTCON_PARITYSEL_NONE  ((uint16_t)0x0000)

/*
#define UARTCON_PARITYEN_MASK	   ((uint16_t)0x0010)
#define UARTCON_PARITYEN_EN	       ((uint16_t)0x0010)
#define UARTCON_PARITYEN_DIS	   ((uint16_t)0x0000)*/

#define UARTCON_RXTX_MASK          ((uint16_t)0x000f)

#define UARTCON_RXIE_MASK          ((uint16_t)0x0008)
#define UARTCON_RXIE_EN            ((uint16_t)0x0008)
#define UARTCON_RXIE_DIS           ((uint16_t)0x0000)

#define UARTCON_TXIE_MASK          ((uint16_t)0x0004)
#define UARTCON_TXIE_EN            ((uint16_t)0x0004)
#define UARTCON_TXIE_DIS           ((uint16_t)0x0000)

#define UARTCON_RXEN_MASK          ((uint16_t)0x0002)
#define UARTCON_RXEN_EN            ((uint16_t)0x0002)
#define UARTCON_RXEN_DIS           ((uint16_t)0x0000)

#define UARTCON_TXEN_MASK          ((uint16_t)0x0001)
#define UARTCON_TXEN_EN            ((uint16_t)0x0001)
#define UARTCON_TXEN_DIS           ((uint16_t)0x0000)

/** Enable selection. */
typedef enum
{
  /** Disable both receiver and transmitter. */
  UART_Disable  = 0x0,
  /** Enable receiver only, transmitter disabled. */
   UART_EnableRxEn = UARTCON_RXEN_EN,
  /** Enable receiver only, transmitter disabled. */
  UART_EnableRx = UARTCON_RXEN_EN | UARTCON_RXIE_EN,
  /** Enable transmitter only, receiver disabled. */
  UART_EnableTx = UARTCON_TXEN_EN | UARTCON_TXIE_EN,
  /** Enable both receiver and transmitter. */
  UART_Enable   = UARTCON_RXTX_MASK
} UART_EnableTypeDef;

/*ISO7816CON*/
#define ISO7816CON_PARITY_MASK ((uint16_t)0x0200)
#define ISO7816CON_PARITY_ODD  ((uint16_t)0x0200)
#define ISO7816CON_PARITY_EVEN ((uint16_t)0x0000)

#define ISO7816CON_AUTORXEN_MASK  ((uint16_t)0x0100)
#define ISO7816CON_AUTORXEN_EN    ((uint16_t)0x0100)
#define ISO7816CON_AUTORXEN_DIS   ((uint16_t)0x0000)

#define ISO7816CON_AUTOTXEN_MASK  ((uint16_t)0x0080)
#define ISO7816CON_AUTOTXEN_EN    ((uint16_t)0x0080)
#define ISO7816CON_AUTOTXEN_DIS   ((uint16_t)0x0080)

#define ISO7816CON_REPTR_MASK ((uint16_t)0x0060)
#define ISO7816CON_REPTR_0    ((uint16_t)0x0000)
#define ISO7816CON_REPTR_1    ((uint16_t)0x0020)
#define ISO7816CON_REPTR_2    ((uint16_t)0x0040)
#define ISO7816CON_REPTR_3    ((uint16_t)0x0060)

#define ISO7816CON_ACKLEN_MASK 	  ((uint16_t)0x0018)
#define ISO7816CON_ACKLEN_1BIT 	  ((uint16_t)0x0000)
#define ISO7816CON_ACKLEN_1P5BIT  ((uint16_t)0x0008)
#define ISO7816CON_ACKLEN_2BIT    ((uint16_t)0x0010)

#define ISO7816CON_RXTXPRD_MASK  ((uint16_t)0x0007)

#define ISO7816CON_PRDIE_MASK    ((uint16_t)0x0004)
#define ISO7816CON_PRDIE_EN      ((uint16_t)0x0004)
#define ISO7816CON_PRDIE_DIS     ((uint16_t)0x0000)

#define ISO7816CON_RXIE_MASK     ((uint16_t)0x0002)
#define ISO7816CON_RXIE_EN       ((uint16_t)0x0002)
#define ISO7816CON_RXIE_DIS      ((uint16_t)0x0000)

#define ISO7816CON_TXIE_MASK   	   ((uint16_t)0x0001)
#define ISO7816CON_TXIE_EN   	   ((uint16_t)0x0001)
#define ISO7816CON_TXIE_DIS   	   ((uint16_t)0x0000)

/** Enable selection. */
typedef enum
{
  ISO7816_Disable  = 0x0,
  ISO7816_EnableRx = ISO7816CON_RXIE_EN,
  ISO7816_EnableTx = ISO7816CON_TXIE_EN,
  ISO7816_EnablePrd   = ISO7816CON_PRDIE_EN
} ISO7816_EnableTypeDef;

/*UARTSTA*/
#define UARTSTA_PARITY_MASK   	   ((uint16_t)0x0004)
#define UARTSTA_PARITY_ERROR   	   ((uint16_t)0x0004)
#define UARTSTA_PARITY_CORRECT     ((uint16_t)0x0000)

#define UARTSTA_RXIF_MASK   	   ((uint16_t)0x0002)
#define UARTSTA_RXIF_DONE   	   ((uint16_t)0x0002)
#define UARTSTA_RXIF_UNFINISH      ((uint16_t)0x0000)

#define UARTSTA_TXIF_MASK   	   ((uint16_t)0x0001)
#define UARTSTA_TXIF_DONE   	   ((uint16_t)0x0001)
#define UARTSTA_TXIF_UNFINISH      ((uint16_t)0x0000)

/*ISO7816STA*/
#define ISO7816STA_RXERRSTAT_MASK     ((uint16_t)0x0010)
#define ISO7816STA_RXERRSTAT_ERROR    ((uint16_t)0x0010)
#define ISO7816STA_RXERRSTAT_CORRECT  ((uint16_t)0x0000)

#define ISO7816STA_TXERRSTAT_MASK     ((uint16_t)0x0008)
#define ISO7816STA_TXERRSTAT_ERROR    ((uint16_t)0x0008)
#define ISO7816STA_TXERRSTAT_CORRECT  ((uint16_t)0x0000)

#define ISO7816STA_PRDIF_MASK     ((uint16_t)0x0004)
#define ISO7816STA_PRDIF_OVER     ((uint16_t)0x0004)
#define ISO7816STA_PRDIF_UNOVER   ((uint16_t)0x0000)

#define ISO7816STA_RXIF_MASK     ((uint16_t)0x0002)
#define ISO7816STA_RXIF_DONE     ((uint16_t)0x0002)
#define ISO7816STA_RXIF_UNFINISH ((uint16_t)0x0000)

#define ISO7816STA_TXIF_MASK     ((uint16_t)0x0001)
#define ISO7816STA_TXIF_DONE     ((uint16_t)0x0001)
#define ISO7816STA_TXIF_UNFINISH ((uint16_t)0x0000)

/*IRCON*/
#define IRCON_IRLVL_MASK      ((uint16_t)0x0002)
#define IRCON_IRLVL_POSITIVE  ((uint16_t)0x0002)
#define IRCON_IRLVL_NEGATIVE  ((uint16_t)0x0000)

#define IRCON_IRTX_MASK      ((uint16_t)0x0001)
#define IRCON_IRTX_EN  		 ((uint16_t)0x0001)
#define IRCON_IRTX_DIS  	 ((uint16_t)0x0000)

/*IRDUTY*/
#define IRDUTY_IRDUTY_MASK   ((uint16_t)0x0003)
#define IRDUTY_IRDUTY_1F2    ((uint16_t)0x0000)
#define IRDUTY_IRDUTY_1F4    ((uint16_t)0x0001)
#define IRDUTY_IRDUTY_1F8    ((uint16_t)0x0002)
#define IRDUTY_IRDUTY_1F16   ((uint16_t)0x0003)

typedef enum
{
  UART_RxState  = 0x0,
  UART_TxState, 
  UART_ParityState,
} UART_ReadStaDef;

typedef enum
{
  ISO7816_RxState  = 0x0,
  ISO7816_RxErrState,
  ISO7816_TxState,  
  ISO7816_TxErrState, 
  ISO7816_PrdState,    
} ISO7816_ReadStaDef;

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

/* Initialization and Configuration functions *********************************/
void UARTx_Init(HT_UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct, IR_InitTypeDef *IR_InitStruct);
void UARTx_DeInit(HT_UART_TypeDef* UARTx);
void UARTx_StructInit(UART_InitTypeDef* UART_InitStruct, IR_InitTypeDef *IR_InitStruct);
void UARTx_Enable(HT_UART_TypeDef* UARTx, UART_EnableTypeDef UARTx_Type);
void UARTx_Disable(HT_UART_TypeDef* UARTx, UART_EnableTypeDef UARTx_Type);
uint16_t UARTx_ReadState(HT_UART_TypeDef* UARTx, UART_ReadStaDef UART_Type);
void UART_ISO7816_Init(HT_UART_TypeDef* UARTx, ISO7816_InitTypeDef* ISO7816_InitStruct);
void UART_ISO7816_StructInit(ISO7816_InitTypeDef* ISO7816_InitStruct);
void UART_ISO7816_Enable(HT_UART_TypeDef* UARTx, ISO7816_EnableTypeDef ISO7816_Type);
void UART_ISO7816_Disable(HT_UART_TypeDef* UARTx, ISO7816_EnableTypeDef ISO7816_Type);
uint16_t UART_ISO7816_ReadState(HT_UART_TypeDef* UARTx, ISO7816_ReadStaDef ISO7816_Type);
void UART_SendData(HT_UART_TypeDef* USARTx, uint16_t Data);
uint16_t UART_ReceiveData(HT_UART_TypeDef* USARTx);

#ifdef __cplusplus
}
#endif

#endif /* __HT6X1X_UART_H */

